package com.ky.tms.controller;

import com.ky.tms.entity.TmsCost;
import com.ky.tms.entity.TmsInvoice;
import com.ky.tms.entity.TmsOrder;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsCostService;
import com.ky.tms.service.ITmsInvoiceService;
import com.ky.tms.service.ITmsReportService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/tms/report")
public class ReprotController {

    @Autowired
    private ITmsReportService tmsReportService;

    @Autowired
    private ITmsCostService tmsCostService;

    @Autowired
    private ITmsInvoiceService tmsInvoiceService;

    @ApiOperation("订单报表分页数据")
    @PostMapping(value = "page/{pageSize}/{current}")
    public CommonResult incomeAuditPage(@RequestBody TmsOrder tmsOrder, @PathVariable("pageSize") Long pageSize, @PathVariable("current") Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsReportService.reportOrderPage(userName,tmsOrder,pageSize,current));
    }

    @ApiOperation("费用报表分页数据")
    @GetMapping("costReportPage")
    public CommonResult costReprotPage(TmsCost tmsCost, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsCostService.costReportPage(userName, tmsCost,pageSize, current));
    }

    @ApiOperation("开票报表分页数据")
    @GetMapping("invoiceReportPage")
    public CommonResult invoiceReprotPage(TmsInvoice tmsInvoice, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsInvoiceService.invoiceReportPage(userName,tmsInvoice, pageSize, current));
    }

    @ApiOperation("下载订单报表excel")
    @GetMapping("orderExport")
    public void orderExport(TmsOrder tmsOrder, HttpServletRequest request, HttpServletResponse response) throws PredictableException {
        tmsReportService.orderExport(tmsOrder, response);
    }

    @ApiOperation("下载费用报表excel")
    @GetMapping("costExport")
    public void costExport(TmsCost tmsCost, HttpServletRequest request, HttpServletResponse response) throws PredictableException {
        tmsReportService.costExport(tmsCost, response);
    }

    @ApiOperation("利润柱状图")
    @GetMapping("incomeBar")
    public CommonResult incomeBar(TmsOrder tmsOrder){
       return CommonResult.buildSuccess(tmsReportService.incomeBar(tmsOrder));
    }

    @ApiOperation("下载新建订单导入模板")
    @GetMapping("entryOrderTemplate")
    public void entryOrderTemplate(HttpServletResponse response) throws PredictableException{
        tmsReportService.entryOrderTemplate(response);
    }
}
